<?php
/**
 * 门锁管理类
 * comfiger
 * 2018-5-28
 **/
namespace Mama\Model;
use Think\Model;
class LockModel extends Model{
	//定义表名称
	protected $tableName = 'lock';

	/**
	 * 获取锁类型列表
	 **/
	public function getLockTypeList(){
		return M('locktype')->order('type_id asc')->select();
	}

	/**
	 * 注册锁
	 **/
	public function registerLockInfo($data){
		return $this->data($data)->add();
	}

	/**
	 * 锁总数
	 **/
	public function getLockListCount($where=array()){
		return $this->alias('llock')
					->join('left join mama_locktype as locktype on llock.lock_type = locktype.ptype')
					->where($where)
					->count();
	}

	/**
	 * 获取锁列表
	 **/
	public function getLockList($page, $where=array()){
		return $this->alias('llock')
					->join('left join mama_locktype as locktype on llock.lock_type = locktype.ptype')
					->where($where)
					->order('llock.lock_id asc')
					->page($page, C('PAGE_SIZE'))
					->select();
	}

	public function getLockInfoByLockId($lock_id){
		return $this->where(array('lock_id' => $lock_id))->find();
	}

	/**
	 * 编辑锁信息
	 **/
	public function editLockInfoByLockId($lock_id, $data){
		return $this->where(array('lock_id' => $lock_id))->setField($data);
	}

	/**
	 * 删除门锁信息
	 **/
	public function deleteLockInfoByLockId($lock_id){
		return $this->where(array('lock_id' => $lock_id))->delete();
	}

	/** -------------------开锁记录 **/
	public function getLockOpenLogCount($where=array()){
		return M('lock_openlog')->where($where)->count();
	}

	/**
	 * 开锁记录
	 **/
	public function getLockOpenLogList($where=array(),$page){
		return M('lock_openlog')->where($where)->order('opentime desc')->page($page, C('PAGE_SIZE'))->select();
	}

	/** -------------------指令记录 **/
	public function getLockCmdLogCount($where=array()){
		return M('lock_cmdlog')->where($where)->count();
	}

	public function getLockCmdLogList($page, $where=array()){
		return M('lock_cmdlog')->where($where)->order('cmd_time desc')->page($page, C('PAGE_SIZE'))->select();
	}
}